package com.ruoyi.medicine.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.medicine.domain.Cart;
import com.ruoyi.medicine.domain.HisMedicineDrug;
import com.ruoyi.medicine.domain.HisMedicineOrder;
import com.ruoyi.medicine.domain.HisMedicineOrderDetail;
import com.ruoyi.medicine.mapper.HisMedicineCartMapper;
import com.ruoyi.medicine.mapper.HisMedicineOrderDetailMapper;
import com.ruoyi.medicine.mapper.HisMedicineOrderMapper;
import com.ruoyi.medicine.mapper.HisMedicineSupplierMapper;
import com.ruoyi.medicine.service.IHisMedicineOrderService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 采购管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-30
 */
@Service
public class HisMedicineOrderServiceImpl implements IHisMedicineOrderService 
{
    @Autowired
    private HisMedicineOrderMapper hisMedicineOrderMapper;
    @Autowired
    private HisMedicineCartMapper hisMedicineCartMapper;
    @Autowired
    private HisMedicineSupplierMapper hisMedicineSupplierMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private HisMedicineOrderDetailMapper hisMedicineOrderDetailMapper;
    /**
     * 查询采购管理
     * 
     * @param orderId 采购管理主键
     * @return 采购管理
     */
    @Override
    public HisMedicineOrder selectHisMedicineOrderByOrderId(Long orderId)
    {
        return hisMedicineOrderMapper.selectHisMedicineOrderByOrderId(orderId);
    }

    /**
     * 查询采购管理列表
     * 
     * @param hisMedicineOrder 采购管理
     * @return 采购管理
     */
    @Override
    public List<HisMedicineOrder> selectHisMedicineOrderList(HisMedicineOrder hisMedicineOrder)
    {
        List<HisMedicineOrder> hisMedicineOrders = hisMedicineOrderMapper.selectHisMedicineOrderList(hisMedicineOrder);
        for (HisMedicineOrder order:hisMedicineOrders){
            //查询制单人信息
            order.setUser(sysUserMapper.selectUserById(order.getOrderPerson()));
            //查询供应商信息系
            order.setSupplier(hisMedicineSupplierMapper.selectHisMedicineSupplierBySupplierId(order.getSupplierId()));

        }

        return hisMedicineOrders;
    }

    /**
     * 新增采购管理
     * 使用Mybatis的主键回显
     *      *  1作用：
     *      *   mybatis新增的主键是自增的，可以将新增数据的最新主键值获取到
     *      *   并且将获取的主键值赋值给实体类对象的属性中
     * @param
     * @return 结果
     */
    @Transactional
    @Override
    public int insertHisMedicineOrder(Map<String,Object> map, Long userId) throws JsonProcessingException {
        //新增采购单:补全采购详情
        HisMedicineOrder order = new HisMedicineOrder();
        order.setOrderPerson(userId);
        order.setOrderStatus("0");
        order.setSupplierId(Long.valueOf(map.get("supplierId").toString()));
        order.setRemark(map.get("remark").toString());
        order.setCreateTime(new Date());
        order.setOrderNum(IdUtils.orderNum());
        int i = hisMedicineOrderMapper.insertHisMedicineOrder(order);
        //2新增采购详情
        ArrayList<Integer> drugIds= (ArrayList<Integer>) map.get("drugIds");
        //获取当前用户的购物车
        //创建ObjectMapper对象
        ObjectMapper objectMapper=new ObjectMapper();
        //1.查询用户的购物车
        Cart cart = hisMedicineCartMapper.selectHisMedicineCartByUserId(userId);
        HashMap<Long, HisMedicineDrug> cartMap = objectMapper.readValue(cart.getCart(), new TypeReference<HashMap<Long, HisMedicineDrug>>() {});
        //获取要采购相关的药品信息并新增
        for(Integer drugId:drugIds){
            //获取本次需要添加的购物车中的药品
            HisMedicineDrug medicineDrug = cartMap.get(Long.parseLong(drugId.toString()));
            //创建采购详情对象
            HisMedicineOrderDetail detail = new HisMedicineOrderDetail();
            detail.setOrderId(order.getOrderId());
            detail.setCreateTime(new Date());
            detail.setDetailNum(medicineDrug.getCartDrugNum());
            detail.setDrugId(medicineDrug.getDrugId());
            //新增对应的采购项
            hisMedicineOrderDetailMapper.insertHisMedicineOrderDetail(detail);
            //删除对应的采购项
            cartMap.remove(Long.parseLong(drugId.toString()));
        }
        //更新购物车信息
        String s = objectMapper.writeValueAsString(cartMap);
        //重新赋值给购物车对象
        cart.setCart(s);
        hisMedicineCartMapper.updateCartInfoMapper(cart);
        return i;
    }

    /**
     * 修改采购管理
     *
     * @param hisMedicineOrder 采购管理
     * @return 结果
     */
    @Override
    public int updateHisMedicineOrder(HisMedicineOrder hisMedicineOrder)
    {
        hisMedicineOrder.setUpdateTime(DateUtils.getNowDate());
        return hisMedicineOrderMapper.updateHisMedicineOrder(hisMedicineOrder);
    }

    /**
     * 批量删除采购管理
     * 
     * @param orderIds 需要删除的采购管理主键
     * @return 结果
     */
    @Override
    public int deleteHisMedicineOrderByOrderIds(Long[] orderIds)
    {
        return hisMedicineOrderMapper.deleteHisMedicineOrderByOrderIds(orderIds);
    }

    /**
     * 删除采购管理信息
     * 
     * @param orderId 采购管理主键
     * @return 结果
     */
    @Override
    public int deleteHisMedicineOrderByOrderId(Long orderId)
    {
        return hisMedicineOrderMapper.deleteHisMedicineOrderByOrderId(orderId);
    }
}
